/**
 * MQTT协议链接封装
 * @param destination  Topic订阅主题
 * @param callBack     回调函数 Function类型 参数为取到的值
 */
function connectMqtt(destination, callBack) {
    var message;
    if (!window.WebSocket) {
        message = "您的浏览器不支持WebSockets!";
    } else {
        var client;
        //实例化mqtt
        client = new Messaging.Client('172.16.200.10', Number(8083), 'example-14928');
        //在消息到达
        client.onMessageArrived = function (res) {
            message = res.payloadString;
            callBack(message);
        };
        //在连接丢失
        client.onConnectionLost = function (responseObject) {
            if (responseObject.errorCode !== 0) {
                message = responseObject.errorCode;
            }
        };
        client.connect({
            userName: 'admin',
            password: 'password',
            onSuccess: function () {
                client.subscribe(destination);
            },
            onFailure: function (failure) {
                message = failure.errorMessage;
            }
        });
    }
}
//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}
//点击复制
function setCopy(_sTxt) {
    // 创建'虚拟'DOM
    const input = document.createElement('input');
    document.body.appendChild(input);
    input.setAttribute('value', _sTxt);
    input.select();
    if (document.execCommand('copy')) {
        document.execCommand('copy');
    }
    // 删除'虚拟'DOM
    document.body.removeChild(input);
}
//深拷贝
function copyFun(data) {
    if (typeof data == "object") {
        if (data instanceof Array) {
            var newArray = [];
            for (var n = 0; n < data.length; n++) {
                newArray.push(data[n])
            }
            return newArray
        } else {
            var newObject = {};
            for (var item in data) {
                newObject[item] = copyFun(data[item]);
            }
            return newObject
        }
    } else {
        return data
    }
}
//ajax请求
function ajax(url,type,data,callback) {
    $.ajax({
        url: url,
        type: type,
        dataType: "json",
        data: data == null ? '' : JSON.stringify(data),
        async: true,
        contentType: "application/json",
        success: function (response) {
            if (response && response.success == 1) {
                callback(response)
            } else {
                toastr.info(response.error_msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            switch (XMLHttpRequest.status) {
                case(500):
                    toastr.info('服务器系统内部错误');
                    break;
                case(403):
                    toastr.info("无权限执行此操作");
                    break;
                case(408):
                    toastr.info("请求超时");
                    break;
                default:
                    toastr.info(XMLHttpRequest.responseText);
            }
        }
    });
}
//table列表
function MyTable(name,method, url) {
    this.name = name;
    this.method = method;
    this.url = url;
    this.selectedRow = null;
    this.table = $('#' + name);
    this.table.bootstrapTable({
        method: this.method,
        url: this.url,
        dataType: "json",
        cache: false, // 设置为 false 禁用 AJAX 数据缓存， 默认为true
        striped: true,  //表格显示条纹，默认为false
        pagination: true, // 在表格底部显示分页组件，默认false
        pageList: [15, 20], // 设置页面可以显示的数据条数
        pageSize: 16, // 页面数据条数
        pageNumber: 1, // 首页页码
        sidePagination: 'client', // 设置为服务器端分页
        exportDataType: "all",   //导出文件方式  支持: 'basic', 'all', 'selected'. basic：本页数据，all，获取服务器所有数据，selected,本页选择行数据
        showExport: true,  //是否显示导出按钮
        buttonsAlign: "right",  //按钮位置
        clickToSelect: true,
        columns:                 //列表序号
            [{
                //field: 'Number',//可不加
                // title: 'Number',//标题  可不加
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }],
    });
    this.table.on('click-row.bs.table', function (r, row, element) {
        $('.table-line-active').removeClass('table-line-active');
        $(element).addClass('table-line-active');
    });
}
//重新刷新table
MyTable.prototype = {
    constructor: MyTable,
    refresh: function (url) {
        if (url == '')
            this.table.bootstrapTable('refresh');
        else
            this.table.bootstrapTable('refresh', {url: url})
    },
    getFieldByName: function (name) {

    }
}
//树结构
function MyTree(name,plugins, data, callback) {
    for (var i=0;i<data.length;i++){
        data[i].state = {
            "opened": true,          //展示第一个层级下面的node
        }
    }
    this.name = name;
    this.data = data;
    this.tree = $('#' + this.name);
    this.tree.jstree({
        'plugins': [plugins],
        'core': {
            'data':this.data,
            "themes": {
                "dots": false ,      //虚线
                'opened' : true,     //打开第一个节点
            }
        }
    }).bind("activate_node.jstree", function (obj, e) {
        // 处理代码
        // 获取当前节点
        var currentNode = e.node;
        callback(currentNode);
    });
}
//重新tree刷新
MyTree.prototype = {
    constructor: MyTree,
    refresh: function (newData) {
        //绘制新数据
        this.tree.jstree(true).settings.core.data = newData;
        //刷新
        this.tree.jstree(true).refresh();
    }
};

//弹出框提示配置
toastr.options = {
    "closeButton": true,
    "debug": false,
    "progressBar": true,
    "preventDuplicates": false,
    "positionClass": "toast-bottom-right",
    "onclick": null,
    "showDuration": "400",
    "hideDuration": "1000",
    "timeOut": "2000",
    "extendedTimeOut": "1000",
    "showEasing": "swing",
    "hideEasing": "linear",
    "showMethod": "fadeIn",
    "hideMethod": "fadeOut"
};